using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class ZombieGeneral : IEnemy
{
    bool isAnimationPlaying = false;
    int BornHp;
    private void Awake()
    {
        GameFacade.Instance.GameStart = false;
    }
    private void Start()
    {
        
        Init();
        mAttr = GameFacade.Instance.attrFactory.GetAttr(AttrType.warrior);
        MaxHP = mAttr.BornHp;
        BornHp = MaxHP;
        mName = mAttr.Name;
        Attacknumber = mAttr.BornAttack;
        AttackRange = mAttr.AttackRange;
    }
    public override void GetHit(int damage)
    {
        if (MaxHP <= 0)
        {
            return;
        }
        audioSource.clip = GameFacade.Instance.assetsFactory.LoadAudioclip("EnemyGetHit");
        audioSource.Stop();
        audioSource.Play();
        mAnimator.SetTrigger("GetHit");
      
   
        MaxHP -= damage;
        if(MaxHP<=0)
        {
            mAnimator.SetTrigger("Die");
            Destroy(gameObject, 5);
            mAgent.destination = transform.position;
            GameFacade.Instance.characterSystem.RemoveEnemy(this);
        }
    }

    public override void InAttack()
    {
        mAgent.destination = transform.position;
        mAnimator.SetBool("Attack", true);
       
    }

    public override void InChase()
    {
      
        mAnimator.SetBool("Move", true);
    }

    public override void InIdle()
    {
        mAgent.destination = transform.position;
        mAnimator.SetBool("Move", false);
    }

    public override void LeaveAttack()
    {
      
        mAnimator.SetBool("Attack", false);
    }

    public override void LeaveChase()
    {
       
    }

    public override void LeaveIdle()
    {
       
    }

    public override void UpdateAttack()
    {
      
        float number = mAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime % 1;
        isAnimationPlaying = number < 0.95f ? false : true;
        if (distance > 2f&&!isAnimationPlaying)
        {
            mSystem.PerformTransition(e_Enemystate.SeeEnemy);
        }
    }

    public override void UpdateChase()
    {
        //Debug.Log(11);
        float number = mAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime % 1;
        isAnimationPlaying = number < 0.95f ? false : true;
        mAgent.destination = GameFacade.Instance.characterSystem.player.transform.position;
        if (distance >12 && !isAnimationPlaying)
        {
            mSystem.PerformTransition(e_Enemystate.NoEnemy);
        }
        if(distance<1f && !isAnimationPlaying)
        {
            mSystem.PerformTransition(e_Enemystate.CanAttack);
        }
    }

    public override void UpdateIdle()
    {
       
        if (distance <= 8 && !isAnimationPlaying)
        {
            mSystem.PerformTransition(e_Enemystate.SeeEnemy);
        }
    }

    public override void Attack()
    {
      
        GameFacade.Instance.characterSystem.player.GetHit(Attacknumber);
      
    }
   public void SeePlayer()
    {
        transform.LookAt(GameFacade.Instance.characterSystem.player.transform.position);
    }
}
